package qc.module.platform.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import qc.common.core.enums.ResourceStatusFlagEnum;
import qc.common.core.exception.QCPromptException;
import qc.module.platform.dto.config.ConfigDto;
import qc.module.platform.dto.config.ConfigQueryConditionDto;
import qc.module.platform.dto.project.ProjectDto;
import qc.module.platform.entity.QcProjectConfigs;
import qc.module.platform.mapper.ProjectConfigsMapper;
import qc.module.platform.repository.QcProjectConfigsRepository;

import java.util.List;

/**
 * ProjectConfigsService
 *
 * @author QuCheng Tech
 * @create 2023/2/6
 */

@Service
public class ProjectConfigsService {
    private QcProjectConfigsRepository repository;
    private ProjectService projectService;

    @Autowired
    public void setQcProjectConfigsRepository(QcProjectConfigsRepository repository) {
        this.repository = repository;
    }

    @Autowired
    public void setProjectService(ProjectService projectService) {
        this.projectService = projectService;
    }

    /**
     * 查询配置信息
     *
     * @param dto     ConfigQueryConditionDto
     * @param isValid 项目状态
     * @return 配置信息集合
     * @author QcCheng Tech
     * @since 2023/2/7
     */
    public List<ConfigDto> query(ConfigQueryConditionDto dto, boolean isValid) throws Exception {
        if (dto == null)
            throw new QCPromptException("传入对象为空");
        if (StringUtils.isBlank(dto.getProject()))
            throw new QCPromptException("项目编号不能为空");

        LambdaQueryWrapper<QcProjectConfigs> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(QcProjectConfigs::getProjectcode, dto.getProject());
        //项目状态和配置信息状态必须为启用
        if (isValid) {
            ProjectDto projectDto = projectService.get(dto.getProject(), false);
            if (projectDto == null)
                throw new QCPromptException("项目不存在");
            if (!projectDto.getFlag().equals(ResourceStatusFlagEnum.NORMAL))
                throw new QCPromptException("项目未启用");

            wrapper.eq(QcProjectConfigs::getFlag, true);
        }
        //指定了建编码则分组编码不起作用
        if (!StringUtils.isBlank(dto.getGroup()) && StringUtils.isBlank(dto.getKey()))
            wrapper.eq(QcProjectConfigs::getGroupcode, dto.getGroup());
        if (!StringUtils.isBlank(dto.getKey()))
            wrapper.eq(QcProjectConfigs::getKeycode, dto.getKey());
        //按排序号降序排序
        wrapper.orderByAsc(QcProjectConfigs::getOdr);

        List<QcProjectConfigs> listConfigs = repository.selectList(wrapper);

        if (listConfigs.size() > 0x0)
            return ProjectConfigsMapper.MAPPER.toDtoLsit(listConfigs);

        return null;
    }

    /**
     * 获取指定配置信息
     *
     * @param dto     ConfigQueryConditionDto
     * @param isValid 项目状态
     * @return 配置信息
     * @author QcCheng Tech
     * @since 2023/2/7
     */
    public ConfigDto getConfigInfo(ConfigQueryConditionDto dto, boolean isValid) throws Exception {
        if (dto == null)
            throw new QCPromptException("传入对象为空");
        if (StringUtils.isBlank(dto.getProject()))
            throw new QCPromptException("项目编码不能为空");
        if (StringUtils.isBlank(dto.getKey()))
            throw new QCPromptException("键编码不能为空");

        LambdaQueryWrapper<QcProjectConfigs> wrapper = new LambdaQueryWrapper<>();

        wrapper.eq(QcProjectConfigs::getProjectcode, dto.getProject()).eq(QcProjectConfigs::getKeycode, dto.getKey());
        //项目状态和配置信息状态为启用
        if (isValid) {
            ProjectDto projectDto = projectService.get(dto.getProject());
            if (projectDto == null)
                throw new QCPromptException("项目不存在");
            if (!projectDto.getFlag().equals(ResourceStatusFlagEnum.NORMAL))
                throw new QCPromptException("项目未启用");

            wrapper.eq(QcProjectConfigs::getFlag, true);
        }

        QcProjectConfigs qcProjectConfigs = repository.selectOne(wrapper);

        if (qcProjectConfigs == null)
            throw new QCPromptException("配置信息不存在");

        return ProjectConfigsMapper.MAPPER.toDto(qcProjectConfigs);
    }
}
